package cn.cvs.controller;

import cn.cvs.entity.Rent;
import cn.cvs.service.RentService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/rent")
public class RentController {
    @Autowired
    private RentService service;
    @RequestMapping("/list")
    public String list(Model model,@RequestParam(required = false) String userName,@RequestParam(defaultValue = "1") int pageIndex){
        int pageSize = 3;
        int pageCount = service.pageCount(pageSize);
        List<Rent> list = service.listByUserName(pageIndex,pageSize,userName);
        model.addAttribute("userName",userName);
        model.addAttribute("list",list);
        model.addAttribute("pageIndex",pageIndex);
        model.addAttribute("pageCount",pageCount);
        return "index";
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }
    @ResponseBody
    @RequestMapping("/update")
    public Object update(int id,int status){
        String str = "true";
        int r = service.update(id,status);
        if (r < 0){
            str = "false";
        }
        String data = JSON.toJSONString(str);
        return data;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(int id){
        String str = "true";
        int r = service.delete(id);
        if (r < 0){
            str = "false";
        }
        String data = JSON.toJSONString(str);
        return data;
    }
}
